package com.wwwc.index.web.servlet;

import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.ServletRequestWrapper;

public class GenerateImageIndexServlet extends HttpServlet {
    public void doGet(HttpServletRequest request,  HttpServletResponse response)
	throws IOException, ServletException
    {
	doPost(request,response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
    {
	try {
	    response.setContentType("text/html");
	    HttpSession session = request.getSession(true);
	    PrintWriter out     = response.getWriter();
	    String user_ip      = request.getRemoteAddr();

	    String call_path = request.getContextPath()+request.getServletPath();
	    String mid       = (String)request.getAttribute("mid");
	    String action    = request.getParameter("aid");
	    String options   = request.getParameter("options");

	    if(action == null) {
		out.println("<FORM method=post action="+call_path+">");
		out.println("<TABLE border=1 width=100%>");
		out.println("<TR><TD align=center colspan=2>This is a utility for generate a directory index</TD></TR>");
		out.println("<TR><TD align=center>Title</TD>");
		out.println("<TD><input type=text name=title size=90></TD></TR>");
		out.println("<TR><TD align=center>Directory</TD>");
		out.println("<TD><input type=text name=dir size=90></TD></TR>");
		out.println("<TR><TD></TD><TD><input type=submit name=B value=Submit>");
		out.println("<input type=reset name=R value=Reset></TD></TR>");
		out.println("<input type=hidden name=mid value="+mid+">");
		out.println("<input type=hidden name=aid value=0>");
		out.println("</TABLE>");
		out.println("</FORM>");

		out.println("<FORM method=post action="+call_path+">");
		out.println("<TABLE border=1 width=100%>");
		out.println("<TR><TD align=center colspan=2>This is a utility for generate a directory index frame</TD></TR>");
		out.println("<TR><TD align=center>Directory</TD>");
		out.println("<TD><input type=text name=dir size=90></TD></TR>");
		out.println("<TR><TD></TD><TD><input type=submit name=B value=Submit>");
		out.println("<input type=reset name=R value=Reset></TD></TR>");
		out.println("<input type=hidden name=mid value="+mid+">");
		out.println("<input type=hidden name=aid value=1>");
		out.println("</TABLE>");
		out.println("</FORM>");
	    }
	    else if (action.equals("0")) {
		String title = request.getParameter("title");
		String dir   = request.getParameter("dir");
		if(dir==null || dir.length()<1 || !(new File(dir)).isDirectory()) {
		    out.println("Please enter a diretory.");
		    return;
		}

		File f = new File(dir,"index.html");
		if(f.exists()) {
		    f.delete();
		}

		Vector vd = getFileList(dir);
		out.println(gIndexFile(title, dir, vd));
	    }
	    else if (action.equals("1")) {
		String dir   = request.getParameter("dir");
		if(dir==null || dir.length()<1 || !(new File(dir)).isDirectory()) {
		    out.println("Please enter a diretory.");
		    return;
		}

		File f = new File(dir,"index.html");
		if(f.exists()) {
		    f.delete();
		}

		Vector vd = getDirectoryList(dir);
		gFrameIndexFile(dir, vd);
	    }
	}
	catch (Exception e) {
	    System.out.println("GenerateImageIndexServlet:Error:"+e);
	}
    }

    private Vector getFileList(String dir) {
	Vector v = new Vector();
	File flist[] = null;
	if(containsFile(dir)) {
	    flist = (new File(dir)).listFiles();
	    for (int i=0; i<flist.length;i++) {
		if(flist[i].isFile()) {
		    v.add(flist[i].getName());
		}
	    }
	}
	return v;
    }

    private boolean containsFile(String dir) {
	File sf[] = (new File(dir)).listFiles();
	for(int i=0; i<sf.length; i++) {
	    if(sf[i].isFile()) {
		return true;
	    }
	}
	return false;
    }

    private Vector  getDirectoryList(String dir) {
	Vector v = new Vector();
	File sf[] = (new File(dir)).listFiles();
	for(int i=0; i<sf.length; i++) {
	    if(sf[i].isDirectory()) {
		v.add(sf[i].getName());
	    }
	}
	return v;
    }
    
    private String gIndexFile(String title, String dir, Vector file_list_v) {
	File f = new File(dir,"index.html");
	if (f.exists()) {
	    try {
		f.delete();
	    }
	    catch (SecurityException e) {
		return ("Can't delete file :"+f.getPath());
	    }
	}

	file_list_v.trimToSize();
	int total_files = file_list_v.size();

	try {
	    RandomAccessFile outputFile = new RandomAccessFile(f, "rw" );
	    outputFile.writeBytes("<HTML><HEAD><TITLE>image index</TITLE>");
	    outputFile.writeBytes("<SCRIPT>");
	    
	    outputFile.writeBytes("var db = new Array("+total_files+");");
	    Enumeration enum = file_list_v.elements();
	    int i=0;
	    while(enum.hasMoreElements()) {
		outputFile.writeBytes("db["+i+"]=\""+enum.nextElement()+"\";");
		i++;
	    }

	    outputFile.writeBytes("function changeImage(m) {");
	    outputFile.writeBytes("    for(i=0;i<db.length;i++){");
	    outputFile.writeBytes("       if(document.pic.src.indexOf(\"/\"+db[i]) != -1){");
	    outputFile.writeBytes("          if(m==1) {");
	    outputFile.writeBytes("             if(i==(db.length-1)) {");
	    outputFile.writeBytes("                 i=-1;");
	    outputFile.writeBytes("             }");
	    outputFile.writeBytes("             document.pic.src=db[(i+1)];");
	    outputFile.writeBytes("             document.forms.f.counter.value=(i+1)+\"/\"+db.length;");
	    outputFile.writeBytes("          }");
	    outputFile.writeBytes("          if(m==-1) {");
	    outputFile.writeBytes("              if(i==0) {");
	    outputFile.writeBytes("                  i = db.length;");
	    outputFile.writeBytes("              }");
	    outputFile.writeBytes("              document.pic.src=db[(i-1)];");
	    outputFile.writeBytes("              document.forms.f.counter.value=(i-1)+\"/\"+db.length;");
	    outputFile.writeBytes("          }");
	    outputFile.writeBytes("          break;");
	    outputFile.writeBytes("       }");
	    outputFile.writeBytes("    }");
	    outputFile.writeBytes("}");

	    outputFile.writeBytes("function resizeImage() {");
	    outputFile.writeBytes("   var w = document.pic.width;");
	    outputFile.writeBytes("   var h = document.pic.height;");
	    outputFile.writeBytes("   while (w>700) {");
	    outputFile.writeBytes("     w = w*0.9;");
	    outputFile.writeBytes("   }");
	    outputFile.writeBytes("  document.pic.width  = w;");
	    outputFile.writeBytes("}");
	    outputFile.writeBytes("</SCRIPT>");
	    outputFile.writeBytes("</HEAD>");
	    outputFile.writeBytes("<BODY><CENTER>");
	   
	    //outputFile.writeBytes("<TABLE border=0 width=800 style='font-size:10pt'>");
	    //outputFile.writeBytes("<TR><TD align=center>"+title+"</TD></TR>");
	    //outputFile.writeBytes("</TABLE>");
	    
	    //outputFile.writeBytes("<TR><TD align=center>");
	    outputFile.writeBytes("<TABLE border=1 width=800 height=500>");
	    outputFile.writeBytes("<FORM name=f>");
	    outputFile.writeBytes("<TR><TD align=center><B>"+title+"</B></TD></TR>");
	    outputFile.writeBytes("<TR><TD align=center>");
	    outputFile.writeBytes("<input type=button name=B1 value=Back style='font-size: 8pt' ");
	    outputFile.writeBytes(" onclick=javascript:changeImage(-1);>");
	    outputFile.writeBytes("<input type=button name=counter value=0/"+total_files);
	    outputFile.writeBytes(" style='font-size: 8pt'>");
	    outputFile.writeBytes("<input type=button name=B2 value=Next style='font-size: 8pt' ");
	    outputFile.writeBytes(" onclick=javascript:changeImage(1);>");
	    outputFile.writeBytes("</TD></TR>");
	    outputFile.writeBytes("</FORM>");
	    outputFile.writeBytes("<TR><TD align=center>");
	    outputFile.writeBytes("<img name=pic src="+file_list_v.elementAt(0)+" border=0");
	    outputFile.writeBytes(" onLoad=javascript:resizeImage();>");
	    outputFile.writeBytes("</TD></TR>");
	    
	    outputFile.writeBytes("</TABLE></CENTER></BODY></HTML>");
	    outputFile.close();
	}
	catch ( IOException e ) {
	    return("GenerateImageIndex:Error:"+e);
	}
	return "Down!";
    }

    private void gFrameIndexFile(String dir, Vector v) {
	File f = new File(dir,"index_left.html");
	while(f.exists()) {
	    f.delete();
	}

	f = new File(dir,"index.html");
	while(f.exists()) {
	    f.delete();
	}

	f = new File(dir,"index_left.html");
	
	try {
	    RandomAccessFile outputFile = new RandomAccessFile(f, "rw" );
	    outputFile.writeBytes("<HTML><HEAD><TITLE>left index</TITLE>");
	    outputFile.writeBytes("</HEAD>");
	    outputFile.writeBytes("<BODY>");
	    outputFile.writeBytes("<BR><BR><BR>");
	    outputFile.writeBytes("<TABLE border=0 style='font-size:12pt'>");

	    Enumeration enum = v.elements();
	    Enumeration enums = null;
	    String temp = null;
	    String temps = null;
	    while(enum.hasMoreElements()) {
		temp = ""+enum.nextElement();
		outputFile.writeBytes("<TR><TD align=left>");
		outputFile.writeBytes("<A href=./"+temp+"/index.html target=main>"+temp.toUpperCase()+"</A></TD></TR>");
	    }
	    outputFile.writeBytes("</TABLE></BODY></HTML>");
	    outputFile.close();
	}
	catch ( IOException e ) {
	    
	}

	f = new File(dir,"index.html");
	
	try {
	    RandomAccessFile outputFile = new RandomAccessFile(f, "rw" );
	    outputFile.writeBytes("<HTML><HEAD><TITLE>image index</TITLE>");
	    outputFile.writeBytes("</HEAD>");
	    outputFile.writeBytes("<frameset cols='125,*' border=1 frameborder=1>");
	    outputFile.writeBytes("<frame name=left scrolling=auto src='./index_left.html'>");
	    outputFile.writeBytes("<frame name=main scrolling=auto src='./"+v.firstElement()+"/index.html'>");
            outputFile.writeBytes("</frameset>");
	    outputFile.writeBytes("</HTML>");
	    outputFile.close();
	}
	catch ( IOException e ) {
	    
	}
    }
}



